/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateful;

import entity.*;
import java.sql.Time;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Dao Thai Duy
 */
@Stateful
@LocalBean
public class TransactionSessionBean {
    
    @PersistenceContext
    private EntityManager entityManager;
    StakeholderSessionBean stakeholderSessionBean;
    
    public List<TransactionsEntity> getAllCustomerTransactions(){
        Query query = entityManager.createQuery("SELECT t FROM TransactionsEntity t");
        return query.getResultList();
    }
    public List<PaymentItemEntity> getAllStakeholderTransactions(){
        Query query = entityManager.createQuery("SELECT p FROM PaymentItemEntity p");
        return query.getResultList();
    }
    public List<TransactionsEntity> getCustomerTransactionsByDate(Date startDate, Date endDate){
        Query query = entityManager.createQuery("SELECT t FROM TransactionsEntity t WHERE t.datePaid >= :startDate AND t.datePaid <= :endDate");
        query.setParameter("startDate", startDate);
        query.setParameter("endDate", endDate);
        return query.getResultList();
    }
    public List<PaymentItemEntity> getStakeholderTransactionsByDate(Date startDate, Date endDate){
        Query query = entityManager.createQuery("SELECT p FROM PaymentItemEntity p WHERE p.datePaid >= :startDate AND p.datePaid <= :endDate");
        query.setParameter("startDate", startDate);
        query.setParameter("endDate", endDate);
        return query.getResultList();
    }
    public void addIntoPaymentManagement(String paymentManagementID, PaymentItemEntity paymentItem){
        PaymentManagementEntity paymentManagement= entityManager.find(PaymentManagementEntity.class, paymentManagementID);
        paymentManagement.setTotalPaymentCurrent(paymentManagement.getTotalPaymentCurrent()+paymentItem.getAmountToStakeholder());
        paymentManagement.getPaymentItem().add(paymentItem);
    }
    public void addTransaction(Double amount, Date datePaid, Time timePaid, ReservationEntity reservation, CustomerEntity customer){
        TransactionsEntity transaction= new TransactionsEntity();
        transaction.setAmount(amount);
        transaction.setCustomer(customer);
        transaction.setDatePaid(datePaid);
        transaction.setTimePaid(timePaid);
        transaction.setReservation(reservation);
        transaction.setCustomer(customer);
        entityManager.persist(transaction);
    }
    
    public List<PaymentItemEntity> getPaymentItemOfStakeholder(StakeholderEntity stakeholder, Date startDate, Date endDate){
        List<PaymentItemEntity> paymentItems;
        paymentItems= stakeholder.getPaymentItem();

//        else{
//            Query query = entityManager.createQuery("SELECT p FROM PaymentItemEntity p WHERE p.id= :id AND p.datePaid >= :startDate AND p.datePaid <= :endDate");
//            query.setParameter("id", stakeholder.getId());
//            query.setParameter("startDate", startDate);
//            query.setParameter("endDate", endDate);
//            paymentItems= query.getResultList();
//        }
        return paymentItems;      
    }
    
    public List<TransactionsEntity> getTransactionsOfCustomer(CustomerEntity customer, Date startDate, Date endDate){
        List<TransactionsEntity> transactions;
//        CustomerEntity customer = entityManager.find(CustomerEntity.class, customerId);
        transactions= customer.getTransactions();

//        else{
//            Query query = entityManager.createQuery("SELECT t FROM TransactionsEntity t WHERE t.CUSTOMER_ID= :id AND t.datePaid >= :startDate AND t.datePaid <= :endDate");
//            query.setParameter("id", customer.getId());
//            query.setParameter("startDate", startDate);
//            query.setParameter("endDate", endDate);
//            transactions= query.getResultList();
//            System.out.println("date1 "+startDate);
//            System.out.println("date2 "+endDate);
//            System.out.println("transaction list size "+transactions.size());
//        }
        return transactions;
    }
    
    public PaymentManagementEntity getPaymentManagementById(Long id){
        return entityManager.find(PaymentManagementEntity.class, id);
    }
    
    public PaymentManagementEntity getCurrentBill(StakeholderEntity stakeholderEntity){
        PaymentManagementEntity currentBill=null;
        List<PaymentManagementEntity> paymentManagementList;
        int i;
        paymentManagementList= stakeholderEntity.getPaymentManagement();
        for (i=0;i<paymentManagementList.size();i++){
            if(!paymentManagementList.get(i).isExpired())
                currentBill=paymentManagementList.get(i);
            else
                currentBill=currentBill;
        }
        return currentBill;
    }
}
